package com.dhl.fin.api.domain;

import com.dhl.fin.api.common.annotation.BusinessId;
import com.dhl.fin.api.common.annotation.ExcelTitle;
import com.dhl.fin.api.common.domain.BasicDomain;
import com.dhl.fin.api.common.util.DateUtil;
import com.dhl.fin.api.common.util.ObjectUtil;
import com.dhl.fin.api.dto.BankCardDataResponseDTO;
import lombok.Data;
import lombok.EqualsAndHashCode;

import javax.persistence.*;
import java.util.Date;
import java.util.List;

/**
 * @author CuiJianbo
 * @since 2025/3/15
 */

/**
 * 银行卡
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Entity
@Table(name = "t_bank_account")
public class BankAccount extends BasicDomain {

    @BusinessId
    @Column(columnDefinition = "varchar(15)")
    private String cardNumber;

    @Column(columnDefinition = "varchar(15)")
    private String cardName;

    @Column(columnDefinition = "varchar(50)")
    private String cardImg;

    @Column(columnDefinition = "varchar(50)")
    private String familyUnicode;

    @Column(columnDefinition = "varchar(10)")
    private String childName;

    @Column(columnDefinition = "decimal(10,2)")
    private Double currentBalance;

    @Column(columnDefinition = "decimal(10,2)")
    private Double totalInterestIncome;

    @Column(columnDefinition = "date")
    private Date expireDate;

    @Column(columnDefinition = "varchar(20)")
    private String status;

    @OneToOne
    @JoinColumn(name = "family_id")
    private Family family;

    @OneToMany(mappedBy = "bankAccount")
    private List<FixDeposit> fixDeposits;

    @OneToMany(mappedBy = "bankAccount")
    private List<LendRecord> lendRecords;

    @Transient
    @ExcelTitle
    private String expireDateStr;


    public BankCardDataResponseDTO changeToBankCardDTO() {
        BankCardDataResponseDTO bankCardDataResponseDTO = new BankCardDataResponseDTO();
        String headPicPath = null;
        if (ObjectUtil.notNull(this.family)) {
            headPicPath = this.family.getHeadPicPath();
        }

        bankCardDataResponseDTO.setId(this.id);
        bankCardDataResponseDTO.setCardImg(this.cardImg);
        bankCardDataResponseDTO.setChildName(this.childName);
        bankCardDataResponseDTO.setStatus(this.status);
        bankCardDataResponseDTO.setHeadPicPath(headPicPath);
        bankCardDataResponseDTO.setCurrentBalance(String.format("%,.2f", this.currentBalance));
        bankCardDataResponseDTO.setTotalInterestIncome(String.format("%,.2f", this.totalInterestIncome));
        bankCardDataResponseDTO.setExpireDateStr(DateUtil.getTime(this.expireDate));

        return bankCardDataResponseDTO;
    }


    public BankCardMoneyReport changeToReportData(int year, int month) {
        BankCardMoneyReport bankCardMoneyReport = new BankCardMoneyReport();
        bankCardMoneyReport.setYear(year);
        bankCardMoneyReport.setMonth(month);
        bankCardMoneyReport.setYearMonthDate(String.format("%s-%02d", year, month));
        bankCardMoneyReport.setCurrentBalance(this.currentBalance);
        bankCardMoneyReport.setCurrentInterestIncome(this.totalInterestIncome);
        bankCardMoneyReport.setBankAccount(this);
        return bankCardMoneyReport;
    }


}
